<template>
  <div>
    <el-form :model="labelForm" ref="labelForm" inline size="small" onsubmit="return false">
      <el-row>
        <el-col :span="6">
          <el-form-item>
            <el-input placeholder="输入标签关键词" v-model="labelForm.labelName" maxlength="30" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" @click="getList(1)">
              <fa-icon class="fa-search" />查询</el-button>
            <el-button plain @click="labelDialog=true">
              <fa-icon class="fa-plus" />添加标签</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <template v-for="(column, index) in LABEL_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" label="操作" class-name="oper-col" :width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="danger" plain :disabled="scope.row.userList && scope.row.userList.length > 0 " @click="delRow(scope.row)">删除</el-button>
            <el-button size="mini" type="primary" plain @click="editRow(scope.row)">编辑成员</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <el-row style="margin-top: 30px;">
      <el-col :span="6">
        <el-button type="" @click="closeLabel('close')">
          取消
        </el-button>
        <el-button type="primary" @click="closeLabel('save')">
          <fa-icon class="fa-check" />保存
        </el-button>
      </el-col>
      <el-col :span="8" :offset="10">
        <pagination :pageNum.sync="labelForm.pageNum" :curSizes.sync="labelForm.pageSize" :total="total" @getList="getList" />
      </el-col>
    </el-row>
    <el-dialog title="添加标签" :visible.sync="labelDialog" width="20%" append-to-body :close-on-click-modal="false">
      <add-label v-if="labelDialog" @uppdataSuccess="uppdataSuccess" @close="close" />
    </el-dialog>
    <el-dialog title="编辑成员" :visible.sync="userDialog" width="60%" append-to-body :close-on-click-modal="false">
      <label-relate-user :editData="editData" v-if="userDialog" @getList="uppdateUser" />
    </el-dialog>
  </div>
</template>

<script>
import { LABEL_HEAD_LIST } from '../js/contant.js'
import { deepCopy } from '@/utils/util'
import { CONSTANT_OBJ } from '@/utils/constant'
import AddLabel from './AddLabel'
import LabelRelateUser from './LabelRelateUser'
import api from '../api'
export default {
  components: { AddLabel, LabelRelateUser },
  data () {
    return {
      LABEL_HEAD_LIST,
      tableLoading: false,
      labelDialog: false,
      userDialog: false,
      dataList: [],
      labelForm: {
        labelName: '',
        pageNum: 1,
        pageSize: 20
      },
      total: 0
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 列表查询
    getList (val) {
      this.labelForm.pageNum = val || this.labelForm.pageNum
      this.tableLoading = true
      api.postQueryLabelData(this.labelForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.dataList.forEach(item => {
            let data = []
            item.userList.forEach(user => {
              data.push(`${user.realName}(${user.userName})`)
            })
            this.$set(item, 'userName', data.join(','))
          })
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 删除数据
    delRow (row) {
      let title = '<p style="font-weight: bold">删除标签确认</p>'
      this.$confirm(title + '请确认是否删除标签？', '提示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delLabel(row)
      }).catch(() => {
        this.showBtn = false
        return false
      })
    },
    delLabel (row) {
      api.deleteLabelData(row.labelId).then(res => {
        const { responseCode, responseMsg } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.$message({
            message: responseMsg,
            type: 'success'
          })
          this.getList()
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    // 编辑
    editRow (row) {
      this.userDialog = true
      this.editData = deepCopy({}, row)
    },
    // 关闭弹框
    uppdataSuccess (data) {
      this[`${data}Dialog`] = false
      this.getList(1)
    },
    // 更新用户标签
    uppdateUser () {
      this.getList()
      this.$emit('uppdateUser')
    },
    closeLabel (val) {
      this.$emit('close', val)
    },
    close () {
      this.labelDialog = false
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../../assets/css/global.less";
.table-top {
  margin: 20px 0 10px;
}
</style>
